package com.qf.service.impl;

import com.qf.dao.GoodsClassiftRepository;
import com.qf.dao.StoreGoodsClassifyRepository;
import com.qf.pojo.entity.GoodsClassify;
import com.qf.pojo.entity.StoreGoodsClassify;
import com.qf.pojo.req.GoodsClassifyReq;
import com.qf.pojo.resp.BaseResp;
import com.qf.service.GoodsClassifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.Id;

/**
 * @ClassName GoodsClassifyServiceImpl
 * @Description TODO
 * @Author Windy
 * @Date 2022/10/5 15:50
 * @Version 1.0
 **/
@Service
public class GoodsClassifyServiceImpl implements GoodsClassifyService {
    @Autowired
    GoodsClassiftRepository goodsClassiftRepository;

    @Autowired
    StoreGoodsClassifyRepository storeGoodsClassifyRepository;

    @Override
    public BaseResp deletegoodsClassify(int id,int storeId) {
        //删除分类信息 不可删除分类信息，因为可能有其他店铺使用该分类
//        goodsClassiftRepository.deleteById(id);
        //删除店铺和分类信息间的关系
        storeGoodsClassifyRepository.deleteByClassifyIdAndStoreId(id,storeId);
        return new BaseResp().SUCCESS();
    }

    @Override
    public BaseResp updateAndAdd(GoodsClassifyReq goodsClassifyReq) {
        //获取商店id
        int storeId=goodsClassifyReq.getStoreId();
        //创建分类对象
        GoodsClassify goodsClassify = new GoodsClassify();
        goodsClassify.setId(goodsClassifyReq.getId());
        goodsClassify.setFoodClassifyName(goodsClassifyReq.getFoodClassifyName());
        if (goodsClassifyReq.getId() == null) {
            goodsClassify.setId(0);
            //添加分类

            GoodsClassify save = goodsClassiftRepository.saveAndFlush(goodsClassify);
            //添加分类与店铺间的关系
            StoreGoodsClassify storeGoodsClassify = new StoreGoodsClassify();
            storeGoodsClassify.setId(0);
            storeGoodsClassify.setStoreId(storeId);
            storeGoodsClassify.setGoodsClassifyId(save.getId());
            storeGoodsClassifyRepository.saveAndFlush(storeGoodsClassify);
            
        }else {
            goodsClassiftRepository.saveAndFlush(goodsClassify);
        }
        return new BaseResp().SUCCESS();
    }
}
